{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<Secrets::DestinationHeader @destination={{@destination}} @refreshList={{this.refreshRoute}} />

{{#if @associations.meta.filteredTotal}}
  <div class="has-bottom-margin-s">
    {{#each @associations as |association index|}}
      <ListItem as |Item|>
        <Item.content>
          <div>
            <Hds::TooltipButton @text="KV v2 engine mount path">
              <Hds::Badge @text="{{association.mount}}/" />
            </Hds::TooltipButton>
            <LinkToExternal
              data-test-association-name={{index}}
              class="has-text-black has-text-weight-semibold"
              @route="kvSecretOverview"
              @models={{array association.mount association.secretName}}
            >{{association.secretName}}</LinkToExternal>
            {{#if association.subKey}}
              <Hds::Badge @text="secret key: {{association.subKey}}/" />
            {{/if}}
            <div>
              <SyncStatusBadge @status={{association.syncStatus}} data-test-association-status={{index}} />
              <code class="has-text-grey is-size-8" data-test-association-updated={{index}}>
                last updated on
                {{date-format association.updatedAt "MMMM do yyyy, h:mm:ss a"}}
              </code>
            </div>
          </div>
        </Item.content>

        <Item.menu>
          <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" @width="210px" as |dd|>
            <dd.ToggleIcon
              @icon="more-horizontal"
              @text="Synced secret popup menu"
              @hasChevron={{false}}
              data-test-popup-menu-trigger
            />
            {{#if (or association.setAssociationPath.isLoading association.removeAssociationPath.isLoading)}}
              <dd.Generic class="has-text-center">
                <LoadingDropdownOption />
              </dd.Generic>
            {{else}}
              {{#if (eq @destination.granularity "secret-key")}}
                <dd.Description
                  @text='Sync or unsync actions will apply to the secret "{{association.secretName}}" and not this individual key.'
                />
                <dd.Separator />
              {{/if}}
              {{#if association.canSync}}
                <dd.Interactive data-test-association-action="sync" {{on "click" (fn this.update association "set")}}>
                  Sync now</dd.Interactive>
              {{/if}}
              <dd.Interactive
                data-test-association-action="view"
                @route="kvSecretOverview"
                @isRouteExternal={{true}}
                @models={{array association.mount association.secretName}}
              >View secret</dd.Interactive>
              {{#if association.canUnsync}}
                <dd.Interactive
                  data-test-association-action="unsync"
                  @color="critical"
                  {{on "click" (fn (mut this.secretToUnsync) association)}}
                >Unsync</dd.Interactive>
              {{/if}}
            {{/if}}
          </Hds::Dropdown>
        </Item.menu>
      </ListItem>
    {{/each}}
    <Hds::Pagination::Numbered
      @currentPage={{@associations.meta.currentPage}}
      @currentPageSize={{@associations.meta.pageSize}}
      @route="secrets.destinations.destination.secrets"
      @model={{@destination}}
      @showSizeSelector={{false}}
      @totalItems={{@associations.meta.filteredTotal}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  </div>
{{else}}
  <EmptyState
    @title="No synced secrets yet"
    @message="Select secrets from existing KV version 2 engines and sync them to the destination."
  >
    <Hds::Link::Standalone
      @text="Sync secrets"
      @icon="chevron-right"
      @iconPosition="trailing"
      @route="secrets.destinations.destination.sync"
    />
  </EmptyState>
{{/if}}

{{#if this.secretToUnsync}}
  <ConfirmModal
    @color="critical"
    @confirmMessage='The secret "{{this.secretToUnsync.secretName}}" will be unsynced from this destination.'
    @onClose={{fn (mut this.secretToUnsync) null}}
    @onConfirm={{fn this.update this.secretToUnsync "remove"}}
  />
{{/if}}